<?php

namespace app\web\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;

class CenterController extends Controller
{
    public function index()
    {
         if (!Session::has('user_tel')) {
            session::delete('user_tel');
            session::delete('userid');
            return view('index/account');
        }elseif(Cookie::has('user_tel')){
            $tel = cookie::get('user_tel');
            $data = Db::name('user')->field('pass', true)->where('tel',$tel)->find();
            // var_dump($data);die;
            $res = Db::table('vip') ->field('level, payorder') ->where('userid', cookie::get('userid')) ->find();
            $level = Db::table('level') ->field('name') ->where('level', $res['level']) ->find();
            $data['levelname'] = $level['name'];
            $data['payorder'] = $res['payorder'];
            return view('center/center',['data'=>$data]);
        }else{
            // 个人中心
            $tel = session::get('user_tel');
            $data = Db::name('user')->field('pass', true)->where('tel',$tel)->find();
            // var_dump($data);die;
            $res = Db::table('vip') ->field('level, payorder') ->where('userid', session::get('userid')) ->find();
            $level = Db::table('level') ->field('name') ->where('level', $res['level']) ->find();
            $data['levelname'] = $level['name'];
            $data['payorder'] = $res['payorder'];
            return view('center/center',['data'=>$data]);
        }
    }

    public function uedit()
    {
        $data = Db::table('user') ->where('id',session::get('userid')) ->find();
        // var_dump($data);
        return view('center/edit',[
            'data' => $data
        ]);
    }

     public function doedit()
     {
        if (!empty(Session::get('userid'))) {
            $userid = session::get('userid');
        }else{
            $userid = cookie::get('userid');
        }
        // var_dump(session::get());die;

       $q = request()->post();
        // var_dump($q);die;
        
       $file = request()->file('icon');
       // var_dump($file);die;
       // 移动到框架应用根目录/public/uploads/ 目录下
       if($file){
           $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
           // var_dump($info);die;
           if($info){
               // 成功上传后 获取上传信息
               // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
               $icon=$info->getSaveName();
                $q['icon']= '/uploads/'.$icon;
           }else{
           // 上传失败获取错误信息
             echo $file->getError();
           }
        }
        // var_dump($q['icon']);die;
        // var_dump($q);die;
        if ($q['birthday']=='') {
            unset($q['birthday']);
        }
        $result = Db::name('user')->where('id', $userid) ->update($q);
        if ($result>0) {
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');

        }
     }

    
    public function repwd()
    {
        // var_dump(session::get());die;
        $userid = session::get('userid');
        $data = Db::table('user') ->where('id',$userid) ->find();
        // var_dump($data);die;
         
        return view('center/repwd',[
            'data' => $data
        ]);
    }

    public function edit()
    {
       $q = request()->post();
        if (!empty($q['pass'])) {
            $tel = session::get('user_tel');
            $data = Db::name('user')->field('id,pass')->where('tel',$tel)->find();
            // var_dump($data);
            if (md5($q['pass'])==$data['pass']) {
                $preg='/^\S{6,14}$/';
                if( !preg_match($preg,$q['newpass']) ){
                    return $this->error('新密码格式不符');
                }
                if ($q['newpass']==$q['repass']) {
                    $pass['pass']=md5($q['newpass']);
                    // var_dump($pass);die;
                    $result = Db::name('user')->where('id',$data['id'])->update($pass);
                    if ($result>0) {
                        session::delete('user_tel');
                        session::delete('user_id');
                        return $this->success('修改成功,请重新登录','/logout');
                    }else{
                        return $this->error('修改失败');
                    }
                }else{
                    return $this->error('两次密码不一致');
                }
            }else{
                return $this->error('原密码错误');
            }
        }else{
            return $this->error('输入错误');
        }
    }

     public function uorder()
    {
        $data = Db::table('order') ->where('userid', session::get('userid')) ->select();
        // var_dump($data);die;
         
        for($a=0; $a<count($data); $a++) {
            $res = Db::table('order_goods') ->where('oid', $data[$a]['id']) ->select();
            
            for($b=0; $b<count($res); $b++){
                $det = Db::table('goods') ->field('name, pictrue') ->where('id', $res[$b]['gid']) ->select();
                $res[$b]['name'] = $det[0]['name'];
                $res[$b]['pictrue'] = $det[0]['pictrue'];
            }

            // var_dump($det);die;
            $data[$a]['goodsdetails'] = $res;
        }

        // var_dump($data);die;
        return view('center/uorder',[
            'data' => $data
        ]);
    }

    public function pwss()
    {
        var_dump($_POST);die;
    }

    public function pay($id)
    {

        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;

        $status = ['status'=> 2];
        $ok = Db::table('order') ->where('id', $id) ->update($status);

        $data = Db::table('order')
                    ->field('price')
                    ->where('userid', $userid)
                    ->select();
//        var_dump($data);die;

        $payorder = 0;

        for($p=0; $p<count($data); $p++){
            $payorder += $data[$p]['price'];
        }
//        var_dump($payorder);die;

        if($payorder >= 50){

            $vmess = Db::table('vip') ->field('id') ->where('userid', $userid) ->find();

            $levelmessa = Db::table('level') ->order('payorder') ->select();
//            var_dump($levelmessa);die;

//            if($payorder >= 1000){
//                $vip['level'] = 5;
//            }elseif($payorder >= 800){
//                $vip['level'] = 4;
//            }elseif($payorder >= 600){
//                $vip['level'] = 3;
//            }elseif($payorder >= 400){
//                $vip['level'] = 2;
//            }elseif($payorder >= 200){
//                $vip['level'] = 1;
//            }

//            var_dump($levelmessa);die;

            for($i=0; $i<count($levelmessa); $i++){
                if($payorder >= $levelmessa[$i]['payorder']){
                    $vip['level'] = $levelmessa[$i]['level'];
                }
            }

//            var_dump($vip['level']);die;
            $vip['payorder'] = $payorder;

            $mm = Db::table('user') -> field('nickname, tel') ->where('id', $userid) ->find();

            $vip['userid'] = $userid;
            $vip['name'] = $mm['nickname'];
            $vip['tel'] = $mm['tel'];
            $vip['time'] = 12345;

            if(!empty($vmess)){
                $update = Db::table('vip') ->where('userid', $userid) ->update($vip);
            }else{
                $insert = Db::table('vip') ->insert($vip);
            }
        }
            return $this ->success('支付成功','/uorder');
       
    }

}
